package com.up.upzl.biz.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import java.util.List;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.up.upzl.biz.domain.ArticleCategory;
import com.up.upzl.biz.domain.ArticleCategory.Type;
import com.up.upzl.biz.service.ArticleCategoryService;

/**
 * 新闻资讯管理
 * 
 * @author youke
 *
 */

@Controller
@RequestMapping("articleCategory")
public class ArticleCategoryController {
	@Autowired
	private ArticleCategoryService articleCategoryService;

	/**
	 * 新闻资讯管理-一级页面
	 * 
	 */
	@GetMapping("list")
	public String List(Long parentId, String name, Pageable page, Model model) {
		Page<ArticleCategory> list = articleCategoryService.findByCondition(parentId, name, page);
		model.addAttribute("pageList", list);
		return "articleCategory/list";
	}

	/**
	 * 新闻资讯管理-二级页面
	 * 
	 */
	@GetMapping("subsetList")
	public String subsetList(Long parentId, String name, Pageable page, Model model) {
		ArticleCategory articleCategory = articleCategoryService.findOne(parentId);
		if (StringUtils.isEmpty(articleCategory)) {
			return "Id can`t be null";
		}
		model.addAttribute("parentArticle", articleCategory);

		page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(Direction.DESC, "sort"));
		Page<ArticleCategory> list = articleCategoryService.findByCondition(parentId, name, page);
		model.addAttribute("pageList", list);

		return "articleCategory/subsetList";
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("addPage")
	public String add(Long parentId, Model model) {
		if (parentId == null) {
			model.addAttribute("parentArticle", null);
		} else {
			ArticleCategory articleCategory = articleCategoryService.findOne(parentId);
			model.addAttribute("parentArticle", articleCategory);
		}
		List<ArticleCategory> oneCategory = articleCategoryService.findByParentId(null);
		model.addAttribute("oneCategory", oneCategory);

		Type[] typeEnum = Type.values();
		model.addAttribute("type", typeEnum);

		return "articleCategory/add";
	}

	/**
	 * 保存对象
	 */
	@PostMapping("add")
	public @ResponseBody ArticleCategory save(ArticleCategory articleCategory) {
		return articleCategoryService.save(articleCategory);
	}

	/**
	 * 跳转到更新页面
	 */
	@GetMapping({ "updatePage", "detail" })
	public String update(Long id, Model model) {
		ArticleCategory articleCategory = articleCategoryService.findOne(id);
		model.addAttribute("entity", articleCategory);

		List<ArticleCategory> oneCategory = articleCategoryService.findByParentId(null);
		model.addAttribute("oneCategory", oneCategory);

		Type[] typeEnum = Type.values();
		model.addAttribute("type", typeEnum);

		return "articleCategory/update";
	}

	/**
	 * 更新
	 */
	@PostMapping("update")
	public @ResponseBody ArticleCategory update(ArticleCategory articleCategory) {
		Assert.notNull(articleCategory.getId(), "Id can`t be null");
		if (articleCategory.getParentArticleCategory() != null) {
			ArticleCategory curCategory = articleCategoryService
					.findOne(articleCategory.getParentArticleCategory().getId());
			articleCategory.setType(curCategory.getType());
		}
		return articleCategoryService.save(articleCategory);
	}

	/**
	 * 删除
	 */
	@PostMapping("delete")
	public @ResponseBody Map<String, String> delete(ArticleCategory articleCategory) {
		Assert.notNull(articleCategory.getId(), "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			articleCategoryService.delete(articleCategory.getId());
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}

}
